package com.tina.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tina.entity.TiLinks;
import com.tina.model.ResultFormat;
import com.tina.model.ResultFormatPaging;
import com.tina.service.TiLinksService;
import com.tina.util.ResultPagingUtil;
import com.tina.util.ResultUtil;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author PIGS
 * @since 2020-06-05
 */
@RestController
@RequestMapping("/tiLinks")
public class TiLinksController {
    private Logger logger = LoggerFactory.getLogger(TiLinksController.class);

    @Autowired
    private TiLinksService tiLinksService;

    /**
     * 查询友情鏈接并分页
     *
     * @param tiLinks
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("queryLinkList")
    @ApiOperation(value = "查询友情鏈接")
    ResultFormatPaging queryLinkList(TiLinks tiLinks, @RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        logger.info("tiLinks={}", tiLinks);
        IPage<TiLinks> tiLinksIPage = tiLinksService.queryLinkList(tiLinks, page, limit);
        List<TiLinks> records = tiLinksIPage.getRecords();
        for (TiLinks record : records) {
            logger.info("record={}", record);
        }
        return ResultPagingUtil.pagingSuccess(0, tiLinksIPage.getTotal(), records);
    }

    /**
     * 添加友情鏈接
     *
     * @return
     */
    @PostMapping("saveLink")
    @ApiOperation(value = "添加友情鏈接")
    public ResultFormat saveLink(TiLinks tiLinks) {
        logger.info("tiLinks={}", tiLinks);
        Integer saveLink = tiLinksService.saveLink(tiLinks);
        logger.info("saveLink={}", saveLink);
        if (saveLink > 0) {
            return ResultUtil.success("操作成功");
        }
        return ResultUtil.success(100, "操作失败");
    }

    /**
     * 编辑友情鏈接
     *
     * @return
     */
    @PutMapping("editLink")
    @ApiOperation(value = "编辑友情鏈接")
    public ResultFormat editLink(TiLinks tiLinks) {
        Integer editLink = tiLinksService.editLink(tiLinks);
        if (editLink > 0) {
            return ResultUtil.success();
        }
        return ResultUtil.success(100, "操作失败");
    }

    /**
     * 批量删除友情链接
     * 状态设置为1
     *
     * @return
     */
    @PutMapping("delBatchLink")
    @ApiOperation(value = "批量删除友情链接")
    public ResultFormat delBatchLink(Integer websiteState, @RequestParam(value = "websiteId[]") Integer[] websiteId) {
        logger.info("websiteState={}", websiteState);

        TiLinks tiLinks = new TiLinks();
        tiLinks.setWebsiteState(1);
        boolean updateById=false;
        for (Integer integer : websiteId) {
            tiLinks.setWebsiteId(integer);
            updateById = tiLinksService.updateById(tiLinks);
        }
        if (updateById){
            return ResultUtil.success();
        }
        return ResultUtil.success(100, "操作失败");
    }
}
